import bcrypt from 'bcryptjs';
import moment from 'moment';

moment.locale('zh-cn'); // 使用中文

const topicStatus = [
	{ key: 'saved', value: '已保存' },
	{ key: 'reviewing', value: '审核中' },
	{ key: 'reviewed', value: '审核成功' },
	{ key: 'reject', value: '拒绝' }];

// 格式化时间
export function formatDate(date, friendly) {
  date = moment(date);

  if (friendly) {
    return date.fromNow();
  }
  return date.format('YYYY-MM-DD HH:mm');
}

export function validateId(str) {
  return (/^[a-zA-Z0-9\-_]+$/i).test(str);
}

export function bhash(str) {
  return bcrypt.hashSync(str, 10);
}

export function bcompare(str, hash) {
  return bcrypt.compareSync(str, hash);
}

export function getTopicStatus() {
  return topicStatus;
}

export function formatTopicDate(topic) {
  const create_at = moment(topic.create_at).fromNow();
  const update_at = moment(topic.update_at).fromNow();
  return { ...topic, create_at, update_at };
}
